/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.demo;

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>Project: app2022 - Stream1
 * <p>Powered by webrx On 2022-01-26 11:27:43
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Stream1 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 6, 2, 5, 11, 3, 7};
        System.out.println(Arrays.toString(nums));
        //数据元素个数
        System.out.println(nums.length);

        //Steam<Integer> max() 返回最大值Optional<Integer>
        System.out.println(Arrays.stream(nums).max().getAsInt());
        System.out.println(Arrays.stream(nums).min().getAsInt());
        //count() 返回long
        System.out.println(Arrays.stream(nums).count());
        //sum() 返回的是int
        System.out.println(Arrays.stream(nums).sum());
        //filter() 过滤 只要偶数，并求和
        System.out.println(Arrays.stream(nums).filter(e -> e % 2 == 0).sum());
        //求出数组的平均
        System.out.println(Arrays.stream(nums).average().getAsDouble());

        var isa = Stream.of(1, 2, 3);  //Stream<Integer>
        var iss = Stream.of("java", "javascript", "mysql");  //Stream<String>

        var n = IntStream.rangeClosed(1, 6);
        //求1*2*3*4*5*6 阶乘 reduce() 归约操作
        System.out.println(n.reduce(1, (a, b) -> a * b));

    }
}
